<script lang="ts" setup>
// import type { DemoTableApi } from '../mock-api';

import type { VxeGridProps } from '#/adapter/vxe-table';

import PictureUpload from '#/components/PictureUpload.vue';

import { useVbenModal } from '@vben/common-ui';

import { ElButton, ElImage } from 'element-plus';

import { useVbenForm } from '#/adapter/form';
import { useVbenVxeGrid } from '#/adapter/vxe-table';
// import { MOCK_API_DATA } from '../table-data';
import { getMaterialApi, PictureAddApi, fileUploadR2 } from '#/api';
import { Material_Type } from '#/api/autoworld/type';
interface RowType {
  category: string;
  color: string;
  id: string;
  price: string;
  productName: string;
  releaseDate: string;
}

// 数据实例
// const MOCK_TREE_TABLE_DATA = [
//   {
//     date: '2020-08-01',
//     id: 10_000,
//     name: 'Test1',
//     parentId: null,
//     size: 1024,
//     type: 'mp3',
//   },
// ]

const sleep = (time = 1000) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

const gridOptions: VxeGridProps<RowType> = {
  checkboxConfig: {
    highlight: true,
    labelField: 'name',
  },
  columns: [
    { title: '序号', type: 'seq', width: 50 },
    { title: 'id' },
    {
      field: 'ossUrl',
      slots: { default: 'ossUrl' },
      title: '缩略图',
    },
  ],
  exportConfig: {},
  // height: 'auto', // 如果设置为 auto，则必须确保存在父节点且不允许存在相邻元素，否则会出现高度闪动问题
  keepSource: true,
  proxyConfig: {
    ajax: {
      query: async ({ page }) => {
        const res = await getMaterialApi({
          type: Material_Type.IMAGE,
          offset: page.currentPage,
          size: page.pageSize,
        });
        return {
          total: res.total,
          items: res.records,
        };
      },
    },
  },
  toolbarConfig: {
    custom: true,
    export: true,
    // import: true,
    refresh: true,
    zoom: true,
  },
};

const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions,
});

const [BaseForm, formApi] = useVbenForm({
  showDefaultActions: false,
  commonConfig: {
    // 所有表单项
    componentProps: {
      class: 'w-full',
    },
  },
  schema: [
    {
      component: 'Select',
      fieldName: 'ossUrl',
      hideLabel: true,
    },
    {
      component: 'Select',
      fieldName: 'tagCodes',
      label: '标签编码',
      componentProps: {
        filterable: true,
        multiple: true,
        options: [
          { value: 'A', label: '选项A' },
          { value: 'B', label: '选项B' },
          { value: 'C', label: '选项C' },
        ],
      },
    },
  ],
});
const [Modal, modalApi] = useVbenModal({
  onConfirm: () => {
    formApi.getValues().then(async (res) => {
      // await PictureAddApi(res);
      modalApi.close();
      // gridApi.reload();
      fileUploadR2(res.file);
    });
  },
});
// 提交
</script>

<template>
  <div class="vp-raw w-full">
    <Grid>
      <template #toolbar-tools>
        <ElButton class="mr-2" type="primary" @click="() => modalApi.open()">
          添加图片
        </ElButton>
      </template>
      <template #ossUrl="{ row }">
        <ElImage
          :preview-src-list="[row.ossUrl]"
          :src="row.ossUrl"
          class="size-4"
          fit="contain"
        />
      </template>
    </Grid>
    <Modal class="w-[600px]" title="添加图片">
      <BaseForm>
        <template #ossUrl>
          <div class="flex w-full items-center justify-center">
            <PictureUpload
              @change="
                (v) => {
                  formApi.setValues({ ossUrl: v.preUrl, file: v.file });
                }
              "
            />
          </div>
        </template>
      </BaseForm>
    </Modal>
  </div>
</template>
